﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.StateMachine.SPI;

namespace T.FAS.Runtime.StateMachine.Engine
{
    class NotSameAsRuleProcess : IRuleProcess
    {
        public const string CSTKEY = "NotSameAs";

        /// <summary>
        /// 待比较匹配的值，如果值为多个，以英文逗号,隔开
        /// </summary>
        public static readonly string ParmKey = "NotMatchValues";

        public string Key => CSTKEY;

        public string Description => "不能与目标值一致";

        private List<ParamInfo> paramList = new List<ParamInfo>();
        public bool IsPreCheck => true;

        public NotSameAsRuleProcess()
        {
            paramList.Add(new ParamInfo() { Name = ParmKey, IsRequired = true, Description = "不匹配的值列表，多个时以逗号分隔" });
        }

        public List<ParamInfo> NeedParamInfo { get { return paramList; } }

        /// <summary>
        /// 与目标值不一致校验废弃，使用表达式规则统一交验
        /// </summary>
        /// <param name="record"></param>
        /// <param name="state"></param>
        /// <param name="rule"></param>
        /// <param name="metadata"></param>
        /// <param name="failReason"></param>
        /// <returns></returns>
        public bool Check(TRecord record, TState state, TRule rule, StateMachineMetadata metadata, StringBuilder failReason)
        {
            //if (record == null || state == null || rule == null)
            //    return true;

            //var target = rule.Target;
            //var data = record.GetValue(target);
            //if (!rule.Paramters.ContainsKey(ParmKey))
            //    throw new Exception($"规则:{CSTKEY}，要求参数{ParmKey}必填!");
            //var dataStr = Convert.ToString(data);
            //var NotMatchValues = rule.Paramters[ParmKey].ToString();

            //if (!string.Equals(dataStr, NotMatchValues, StringComparison.OrdinalIgnoreCase))
            //{
            //    var MatchValueArrays = NotMatchValues.Split(',');
            //    if (MatchValueArrays.Contains(dataStr))
            //    {
            //        failReason.AppendLine(string.Format("字段:{0}，不可以是目标值:{1}", target, NotMatchValues));
            //        return false;
            //    }
            //}
            //else
            //{
            //    failReason.AppendLine(string.Format("字段:{0}，不可以是目标值:{1}", target, NotMatchValues));
            //    return false;
            //}
            return true;
        }
    }
}
